package com.zf.shopping.service.impl;

import com.zf.shopping.bean.GetResult;
import com.zf.shopping.bean.Result;
import com.zf.shopping.dao.CategoryMapper;
import com.zf.shopping.dao.CommodityCategoryMapper;
import com.zf.shopping.dao.CommodityMapper;
import com.zf.shopping.model.*;
import com.zf.shopping.service.CommodityService;
import com.zf.shopping.vo.CommodityV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService{

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CommodityCategoryMapper commodityCategoryMapper;

    /**
     * 添加商品或编辑商品
     * @param commodityV
     * @return
     */
    @Override
    @Transactional
    public Result addOrUpdate(CommodityV commodityV) {

        Commodity commodity = commodityV.getCommodity();
        Category category = commodityV.getCategory();

        try {
            /*保存商品*/
            if (commodity.getId() == 0 || commodity.getId() == null) {
                commodityMapper.insert(commodity);

                //找出刚保存的commodity id
                CommodityExample commodityE = new CommodityExample();
                commodityE.or().andNameEqualTo(commodity.getName());
                List<Commodity> commodities = commodityMapper.selectByExample(commodityE);
                commodity.setId(commodities.get(0).getId());
            } else {
                commodityMapper.updateByPrimaryKey(commodity);
            }

            /*保存类别*/
            if (category.getId() == 0 || category.getId() == null) {
                categoryMapper.insert(category);

                /*找出刚保存的category id*/
                CategoryExample categoryE = new CategoryExample();
                categoryE.or().andNameEqualTo(category.getName());
                List<Category> categories = categoryMapper.selectByExample(categoryE);
                category.setId(categories.get(0).getId());
            } else {
                categoryMapper.updateByPrimaryKey(category);
            }

            CommodityCategoryExample commodityCategoryE = new CommodityCategoryExample();
            commodityCategoryE.or().andCommodityIdEqualTo(commodity.getId());
            //先删除商品与类的关联
            commodityCategoryMapper.deleteByExample(commodityCategoryE);
            //重新保存商品和类的关联
            CommodityCategory commodityCategory = new CommodityCategory();
            commodityCategory.setCategoryId(category.getId());
            commodityCategory.setCommodityId(commodity.getId());
            commodityCategoryMapper.insert(commodityCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }

        return GetResult.success();
    }

    @Override
    @Transactional
    public Result deleteById(Long commodityId) {

        try {
            //删除商品
            commodityMapper.deleteByPrimaryKey(commodityId);
            //删除商品与类别的联系
            CommodityCategoryExample commodityCategoryE = new CommodityCategoryExample();
            commodityCategoryE.or().andCommodityIdEqualTo(commodityId);
            commodityCategoryMapper.deleteByExample(commodityCategoryE);

        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }
        return GetResult.success();
    }

    @Override
    public CommodityV getOneById(Long commodityId) {

        Commodity commodity = commodityMapper.selectByPrimaryKey(commodityId);
        List<Commodity> commodities = new ArrayList<Commodity>();
        commodities.add(commodity);

        return getCommodityV(commodities).get(0);
    }

    @Override
    public List<CommodityV> getAll() {

        CommodityExample commodityE = new CommodityExample();
        commodityE.or().andIdIsNotNull();
        List<Commodity> commodities = commodityMapper.selectByExample(commodityE);

        return getCommodityV(commodities);
    }


    private List<CommodityV> getCommodityV(List<Commodity> commodities) {

        List<CommodityV> commodityVList = new ArrayList<>();
        CommodityCategoryExample commodityCategoryE = new CommodityCategoryExample();

        for (Commodity c: commodities
             ) {
            commodityCategoryE.clear();
            commodityCategoryE.or().andCommodityIdEqualTo(c.getId());
            List<CommodityCategory> commodityCategories = commodityCategoryMapper.selectByExample(commodityCategoryE);
            Category category = categoryMapper.selectByPrimaryKey(commodityCategories.get(0).getCategoryId());
            CommodityV commodityV = new CommodityV();
            commodityV.setCommodity(c);
            commodityV.setCategory(category);
            commodityVList.add(commodityV);
        }

        return commodityVList;
    }
}
